#include <G4SDManager.hh>

#include "HitsCollection.hh"
#include "PMTDetector.hh"
#include "PMTHit.hh"

PMTDetector::PMTDetector(G4String name) :
	G4VSensitiveDetector(name) {
	hitsCollection = 0;
	collectionName.insert("1");
}

PMTDetector::~PMTDetector() {
	;
}

void PMTDetector::Initialize(G4HCofThisEvent* hcc) {
	hitsCollection = new HitsCollection(SensitiveDetectorName, collectionName[0]);

	G4SDManager* sdMan = G4SDManager::GetSDMpointer();

	G4int collectionID = sdMan->GetCollectionID(hitsCollection);
	hcc->AddHitsCollection(collectionID, hitsCollection);
}

void PMTDetector::EndOfEvent(G4HCofThisEvent*) {
	;
}

G4bool PMTDetector::ProcessHits(G4Step *step, G4TouchableHistory*) {
	G4Track* track = step->GetTrack();
	double time = track->GetGlobalTime();
	double deposit = step->GetTotalEnergyDeposit();
	if (deposit == 0.0)
		return false;

	PMTHit* hitt = new PMTHit(time, deposit);
	hitsCollection->Insert(hitt);

	return true;
}
